package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.KnowledgeFoodMapper;
import com.itheima.pojo.KnowledgeFood;
import com.itheima.service.KnowledgeFoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = KnowledgeFoodService.class)
@Transactional
public class KnowledgeFoodServiceImpl implements KnowledgeFoodService {
    @Autowired
    private KnowledgeFoodMapper knowledgeFoodMapper;
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<KnowledgeFood> knowledgeFoods = knowledgeFoodMapper.selectByCondition(queryString);
        long total = knowledgeFoods.getTotal();
        List<KnowledgeFood> result = knowledgeFoods.getResult();
        return new PageResult(total,result);
    }

    @Override
    public void add(KnowledgeFood knowledgeFood) {
        knowledgeFoodMapper.add(knowledgeFood);
    }

    @Override
    public void edit(KnowledgeFood knowledgeFood) {
        knowledgeFoodMapper.edit(knowledgeFood);
    }

    @Override
    public void delete(Integer id) {
        knowledgeFoodMapper.deleteById(id);
    }

    @Override
    public List<KnowledgeFood> findAll() {
        return knowledgeFoodMapper.findAll();
    }
}
